var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index.js');
var usersRouter = require('./routes/users.js');
var shopRouter = require('./routes/shop.js');
var addressRouter = require('./routes/address.js');

var app = express();

// app.all('*', (req, res, next) => {
//   // google需要配置，否则报错cors error
//   res.setHeader('Access-Control-Allow-Credentials', 'true')
//   // 允许的地址,http://127.0.0.1:9000这样的格式
//   res.setHeader('Access-Control-Allow-Origin', req.get('Origin'))
//   // 允许跨域请求的方法
//   res.setHeader(
//     'Access-Control-Allow-Methods',
//     'POST, GET, OPTIONS, DELETE, PUT'
//   )
//   // 允许跨域请求header携带哪些东西
//   res.header(
//     'Access-Control-Allow-Headers',
//     'Origin, X-Requested-With, Content-Type, Accept, If-Modified-Since'
//   )
//   next()
// })


app.all('*',function(req,res,next) {
    res.header("Access-Control-Allow-Origin","*");
    res.header('Access-Control-Allow-Methods','PUT,GET,POST,DELETE,OPTIONS');
    res.header("Access-Control-Allow-Headers","X-Requested-With");
    res.header('Access-Control-Allow-Headers','Content-Type');
    next();
  });
  

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/static', express.static(path.join(__dirname, 'public')));

console.log(__dirname)
app.use('/', indexRouter);

// 模块前缀
app.use('/users', usersRouter);
app.use('/shop', shopRouter)
app.use('/index', indexRouter)
app.use('/address',addressRouter )


// app.use(express.static('public'))
// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
